<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #toolbar>
        <a-button v-if="selectedRowKeys.length > 0" type="primary" preIcon="ant-design:export-outlined" @click="batchHandleProcess"
          >批量处理</a-button
        >
        <a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportProductXls"> 产品导出</a-button>
        <a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportTerminalXls"> 终端导出</a-button>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <ErrorInfoModal @register="registerErrorInfoModel" />
  </div>
</template>

<script lang="ts" name="dps-processingData" setup>
  import { BasicTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { useListPage } from '/@/hooks/system/useListPage';
  import ErrorInfoModal from './modules/ErrorInfoModal.vue';
  import { columns, searchFormSchema } from './ProcessingData.data';
  import { list, queryErrorInfo, deleteData, getExportUrl, batchProcess, reprocess } from './ProcessingData.api';
  import { useGo } from '@/hooks/web/usePage';
  import { useMethods } from '@/hooks/system/useMethods';
  const go = useGo();

  //注册model
  const [registerErrorInfoModel, { openModal: openErrorInfoModal }] = useModal();
  const { handleExportXls } = useMethods();
  //注册table数据
  // eslint-disable-next-line @typescript-eslint/no-unused-vars,no-unused-vars
  const { prefixCls, tableContext, onExportXls } = useListPage({
    tableProps: {
      title: '数据修复列表',
      rowKey: 'dataId',
      api: list,
      columns,
      canResize: false,
      defSort: {
        column: 'dataId',
        order: 'desc',
      },
      searchInfo: {
        proState: 3,
      },
      formConfig: {
        labelWidth: 120,
        schemas: searchFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: true,
        fieldMapToTime: [['createTime', ['createTime_begin', 'createTime_end'], 'YYYY-MM-DD HH:mm:ss']],
      },
      actionColumn: {
        width: 180,
      },
    },
    exportConfig: {
      name: '数据修复列表',
      url: getExportUrl,
    },
  });

  const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;

  function onExportProductXls() {
    handleExportXls('产品数据修复', getExportUrl + '/product');
  }
  function onExportTerminalXls() {
    handleExportXls('终端数据修复', getExportUrl + '/terminal');
  }
  /**
   * 编辑事件
   */
  async function handleDelete(record: Recordable) {
    await deleteData(record, reload);
  }
  const ruleCodeMapping = {
    LfsAscXslxProductTransformRule: {
      apiName: 'queryProductError',
      compPath: '/mds/dealerProduct',
    },
    LfsAscXslxPartnerTransformRule: {
      apiName: 'queryOrgError',
      compPath: '/mds/dealerTerminal',
    },
  };
  /**
   * 详情
   */
  async function handleDetail(record: Recordable) {
    record.apiName = ruleCodeMapping[record.ruleCode].apiName;
    let data = await queryErrorInfo(record);
    data.ruleCode = record.ruleCode;
    openErrorInfoModal(true, data);
  }
  async function handleReprocess(record: Recordable) {
    await reprocess(record, reload);
  }
  /**
   * 批量处理
   */
  async function batchHandleProcess() {
    await batchProcess({ ids: selectedRowKeys.value.join(',') }, reload);
  }
  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        label: '重新处理',
        onClick: handleReprocess.bind(null, record),
      },
      {
        label: '查看',
        onClick: handleDetail.bind(null, record),
      },
    ];
  }
  /**
   * 下拉操作栏
   */
  function getDropDownAction(record) {
    return [
      {
        label: '作废',
        onClick: handleDelete.bind(null, record),
      },
      {
        label: '修复',
        onClick: function () {
          go(ruleCodeMapping[record.ruleCode].compPath);
        },
      },
    ];
  }
</script>
<style scoped></style>
